package net.gxtvsgwt.client.demo.type;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.gxtvsgwt.client.demo.BaseDemo;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Overflow;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LargeMixDemo extends BaseDemo {

	@SuppressWarnings("rawtypes")
	@Override
	public Widget createGxt() {

		com.extjs.gxt.ui.client.widget.form.TextField<String> field0 = new com.extjs.gxt.ui.client.widget.form.TextField<String>();
		field0.setValue("Value 0");
		com.extjs.gxt.ui.client.widget.form.SimpleComboBox<String> field1 = new com.extjs.gxt.ui.client.widget.form.SimpleComboBox<String>();
		field1.add("Value 1");
		field1.setForceSelection(true);
		field1.setLazyRender(false);
		com.extjs.gxt.ui.client.widget.form.CheckBox field2 = new com.extjs.gxt.ui.client.widget.form.CheckBox();
		Label label2 = new Label("Label 2");
		com.extjs.gxt.ui.client.widget.form.Radio field3 = new com.extjs.gxt.ui.client.widget.form.Radio();
		com.extjs.gxt.ui.client.widget.Label label3 = new com.extjs.gxt.ui.client.widget.Label(
				"Label 3");
		com.extjs.gxt.ui.client.widget.button.Button load = new com.extjs.gxt.ui.client.widget.button.Button(
				"Load");
		com.extjs.gxt.ui.client.widget.button.Button reset = new com.extjs.gxt.ui.client.widget.button.Button(
				"Reset");

		com.extjs.gxt.ui.client.widget.HorizontalPanel header = new com.extjs.gxt.ui.client.widget.HorizontalPanel();
		header.add(field0);
		header.add(field1);
		header.add(field2);
		header.add(label2);
		header.add(field3);
		header.add(label3);
		header.add(load);
		header.add(reset);

		TreeDemo treeDemo = new TreeDemo();
		treeDemo.setDataSize(dataSize);
		Widget tree = treeDemo.createGxt();

		ListStore<BaseModelData> listStore = new ListStore<BaseModelData>();
		for (int i = 0; i < dataSize; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Col0", "test" + i + "0");
			map.put("Col1", "test" + i + "1");
			map.put("Col2", "test" + i + "2");
			listStore.add(new BaseModelData(map));
		}
		List<ColumnConfig> columnConfig = new ArrayList<ColumnConfig>();
		columnConfig.add(new ColumnConfig("Col0", "Col0", 100));
		columnConfig.add(new ColumnConfig("Col1", "Col1", 100));
		columnConfig.get(1).setRenderer(new GridCellRenderer() {
			@Override
			public Object render(ModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore store, Grid grid) {
				return new com.extjs.gxt.ui.client.widget.button.Button(
						(String) model.get(property));
			}
		});
		columnConfig.add(new ColumnConfig("Col2", "Col2", 100));
		columnConfig.get(2).setRenderer(new GridCellRenderer() {
			@Override
			public Object render(ModelData model, String property,
					ColumnData config, int rowIndex, int colIndex,
					ListStore store, Grid grid) {
				return new com.extjs.gxt.ui.client.widget.form.CheckBox();
			}
		});
		ColumnModel columnModel = new ColumnModel(columnConfig);
		com.extjs.gxt.ui.client.widget.grid.Grid<BaseModelData> grid = new com.extjs.gxt.ui.client.widget.grid.Grid<BaseModelData>(
				listStore, columnModel);
		grid.setHeight("200px");
		grid.setWidth("300px");
		grid.setLazyRowRender(0);

		HorizontalPanel body = new HorizontalPanel();
		body.add(tree);
		body.add(grid);

		VerticalPanel panel = new VerticalPanel();
		panel.add(header);
		panel.add(body);

		return panel;
	}

	@Override
	public Widget createGwt() {

		TextBox field0 = new TextBox();
		field0.setValue("Value 0");
		ListBox field1 = new ListBox();
		field1.addItem("Value 1");
		CheckBox field2 = new CheckBox();
		Label label2 = new Label("Label 2");
		RadioButton field3 = new RadioButton("field3");
		Label label3 = new Label("Label 3");
		Button load = new Button("Load");
		Button reset = new Button("Reset");

		HorizontalPanel header = new HorizontalPanel();
		header.add(field0);
		header.add(field1);
		header.add(field2);
		header.add(label2);
		header.add(field3);
		header.add(label3);
		header.add(load);
		header.add(reset);

		TreeDemo treeDemo = new TreeDemo();
		treeDemo.setDataSize(dataSize);
		Widget tree = treeDemo.createGwt();

		List<Map<String, Object>> listStore = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < dataSize; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("Col0", "test" + i + "0");
			map.put("Col1", "test" + i + "1");
			map.put("Col2", "test" + i + "2");
			listStore.add(map);
		}
		CellTable<Map<String, Object>> grid = new CellTable<Map<String, Object>>();
		grid.addColumn(new Column<Map<String, Object>, String>(new TextCell()) {
			@Override
			public String getValue(Map<String, Object> object) {
				return (String) object.get("Col0");
			}
		}, "Col0");
		grid.addColumn(
				new Column<Map<String, Object>, String>(new ButtonCell()) {
					@Override
					public String getValue(Map<String, Object> object) {
						return (String) object.get("Col1");
					}
				}, "Col1");
		grid.addColumn(new Column<Map<String, Object>, Boolean>(
				new CheckboxCell()) {
			@Override
			public Boolean getValue(Map<String, Object> object) {
				return false;
			}
		}, "Col2");
		grid.setRowData(listStore);
		grid.setWidth("300px");
		SimplePanel wrapper = new SimplePanel();
		wrapper.add(grid);
		wrapper.setHeight("200px");
		wrapper.getElement().getStyle().setOverflow(Overflow.AUTO);

		HorizontalPanel body = new HorizontalPanel();
		body.add(tree);
		body.add(wrapper);

		VerticalPanel panel = new VerticalPanel();
		panel.add(header);
		panel.add(body);

		return panel;
	}

	@Override
	public String getName() {
		return "Large Mix - Everything";
	}
}
